package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.UsersPermission;
import com.jokerpan.project.mapper.PermissionsMapper;
import com.jokerpan.project.service.PermissionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("permissionsService")
public class PermissionsServiceImpl implements PermissionsService {
    @Resource
    PermissionsMapper permissionsMapper;


    @Override
    public Map<String, Object> queryList(UsersPermission usersPermission) {
        Page<Object> page = PageHelper.startPage(usersPermission.getCurrentPage(), usersPermission.getPageSize());
        List<UsersPermission> resultList = this.permissionsMapper.queryList(usersPermission);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), usersPermission.getCurrentPage(), usersPermission.getPageSize());
    }

    @Override
    public List<UsersPermission> queryAll() {
        return this.permissionsMapper.queryAll();
    }

    @Override
    public Boolean add(UsersPermission usersPermission) {
        return permissionsMapper.add(usersPermission) > 0;
    }

    @Override
    public ActionResult update(UsersPermission usersPermission) {
        if (permissionsMapper.update(usersPermission) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @Override
    public ActionResult deleteById(Integer id) {
        if (permissionsMapper.deleteById(id) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }
}
